package leetcode.day;


// 1706. 球会落何处
public class FindBall {

    public static void main(String[] args) {
        FindBall findBall = new FindBall();
        findBall.findBall(new int[][]{{1, 1, 1, -1, -1}, {1, 1, 1, -1, -1},
                {-1, -1, -1, 1, 1}, {1, 1, 1, 1, -1}, {-1, -1, -1, -1, -1}});

    }

    public int[] findBall(int[][] grid) {
        int[] res = new int[grid[0].length];
        for (int i = 0; i < grid[0].length; i++) {
            res[i] = getValue(grid, i);
        }
        return res;
    }

    private int getValue(int[][] grid, int i) {
        int row = 0;
        int c = i;
        while (row < grid.length) {
            int x = c + grid[row][c];
            if (x < 0 || x >= grid[0].length) {
                return -1;
            }
            if (grid[row][c] != grid[row][x]) {
                return -1;
            }
            row++;
            c = x;
        }
        return c;
    }

}
